using MediatR;
using UniversalAdmin.Application.Common;
using UniversalAdmin.Application.DTOs;
using UniversalAdmin.Domain.Interfaces;

namespace UniversalAdmin.Application.Features.Menus.Queries.GetMenuDetail;

public class GetMenuDetailQueryHandler : IRequestHandler<GetMenuDetailQuery, ApiResult<MenuFlatDto>>
{
  private readonly IUnitOfWork _uow;

  public GetMenuDetailQueryHandler(IUnitOfWork uow)
  {
    _uow = uow;
  }

  public async Task<ApiResult<MenuFlatDto>> Handle(GetMenuDetailQuery request, CancellationToken ct)
  {
    var menu = await _uow.MenuRepository.GetByIdAsync(request.Id);
    if (menu == null)
      return ApiResult<MenuFlatDto>.Failure(404, "菜单不存在");

    var dto = new MenuFlatDto
    {
      Id = menu.Id,
      Name = menu.Name,
      Path = menu.Path,
      Icon = menu.Icon,
      ParentId = menu.ParentId,
      Sort = menu.Sort
    };

    return ApiResult<MenuFlatDto>.Success(dto);
  }
}